package com.hotelManagerSystem.controller;


import com.github.pagehelper.PageInfo;
import com.hotelManagerSystem.model.*;
import com.hotelManagerSystem.service.*;
import com.hotelManagerSystem.util.PageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import com.hotelManagerSystem.model.RoomOrder;
import com.hotelManagerSystem.service.ICleaningRecordService;
import com.hotelManagerSystem.service.IRoomOrderService;
import com.hotelManagerSystem.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Cynthia
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/room-order")
public class RoomOrderController {
    //注入业务接口
    @Autowired
    private IRoomOrderService iRoomOrderService;
    @Autowired
    private ICleaningRecordService iCleaningRecordService;
    @Autowired
    private IRoomService iRoomService;
    @Autowired
    private IUserService iUserService;


    /*//查询所有房间预订信息
    @RequestMapping("/customer/list")
    public String queryAllRoomOrder(Model model,
                                    @RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                                    @RequestParam(defaultValue = "8", value = "pageSize") Integer pageSize,
                                    @RequestParam(required = false, value = "orderState") Integer orderState) {
        PageUtil.runStartPage(pageNum, pageSize);
        List<RoomOrder> roomOrders = null;
        if (orderState == null) {
            roomOrders = iRoomOrderService.list();
        } else {
            roomOrders = iRoomOrderService.queryInfoByState(orderState);
        }
        PageInfo pageInfo = PageUtil.getPageInfo(roomOrders);
        model.addAttribute("pageInfo", pageInfo);
        return "/system/customer/roomOrder/roomOrder-info";
    }*/

    //通过 username查询 uid->再查询客户自己的房间预订信息
    @RequestMapping("/customer/customerInfo")
    public String queryUserIdByUsername(Model model,
                                      @RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                                      @RequestParam(defaultValue = "8", value = "pageSize") Integer pageSize,
                                      @RequestParam(required = false, value = "orderState") Integer orderState) {
        //获取当前用户Subject
        Subject currentUser = SecurityUtils.getSubject();
        //获取当前用户username
        String username = (String) currentUser.getSession().getAttribute("username");
        //根据用户名获取 uid
        User user = iUserService.queryByUsername(username);
        List<RoomOrder> roomOrders = null;
        PageUtil.runStartPage(pageNum, pageSize);
        if (orderState==null){
            roomOrders = iRoomOrderService.queryInfoByUsername(user.getUid());
        }else {
            roomOrders = iRoomOrderService.queryInfoByUsernameAndState(user.getUid(),orderState);
        }
        PageInfo pageInfo = PageUtil.getPageInfo(roomOrders);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo",pageInfo);
        return "system/customer/roomOrder/roomOrder-list";
    }

    /*-----查询-----房间预订信息列表-----*/
    @RequestMapping("/admin/list")
    public String list(Model model,
                       @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                       @RequestParam(defaultValue="8",value="pageSize")Integer pageSize,
                       @RequestParam(required = false,value="orderNum_s")String orderNum_s
                       ) {
        PageUtil.runStartPage(pageNum,pageSize);
        List<RoomOrder> roomOrderList_db;
        //内置的方法list
        if(orderNum_s == null){
            roomOrderList_db=iRoomOrderService.list();
        }else {
            roomOrderList_db=iRoomOrderService.queryRoomOrderByOrderNum(orderNum_s);
        }
        //分页对象
        PageInfo pageInfo = PageUtil.getPageInfo(roomOrderList_db);
        model.addAttribute("roomOrderList_db",roomOrderList_db);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("orderNum_s",orderNum_s);
//        System.out.println("===查询房间预订信息列表===list()===");
        return "system/admin/roomOrder/roomOrderList1";
    }


    //更新房间预订状态
    /*@RequestMapping("/customer/updateOrderState")
    @ResponseBody
    public Map<String, Object> updateRecordState(int roomId){
        Map<String, Object> map=new HashMap<>();

        //更新房间预订状态
        RoomOrder roomOrder = iRoomOrderService.getById(roomId);
        int flag = iRoomOrderService.updateStateById(roomOrder.getRoomId());
        if(flag==1){
            map.put("code",200);
            map.put("message","更新成功");
        }else {
            map.put("code",500);
            map.put("message","更新失败");
        }
        return map;
    }*/

    /*-----添加UI-----房间预订信息-----*/
    @RequestMapping("/admin/addUI")
    public String addUI(Model model) {
        return "system/admin/roomOrder/addRoomOrder";
    }

    /*-----添加-----房间预订信息-----*/
//    @RequestMapping("/admin/add")
//    public String add(Model model,RoomOrder roomOrder, String inTime, String outTime){
//        //获取
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime dateTime = LocalDateTime.parse(inTime, dateTimeFormatter);
//        roomOrder.setCheckInTime(dateTime);
//
//        dateTime = LocalDateTime.parse(outTime, dateTimeFormatter);
//        roomOrder.setCheckOutTime(dateTime);
//        System.out.println(roomOrder);
//        //调用业务
//        boolean b=iRoomOrderService.save(roomOrder);
//        //返回roomOrderList页面
//        List<RoomOrder> roomOrderList_db=iRoomOrderService.list();
//        model.addAttribute("roomOrderList_db",roomOrderList_db);
//        System.out.println("===添加房间预订信息===add()===");
//        return "system/admin/roomOrder/roomOrderList";
//    }


    //客户添加预订房间->查看预订信息
    /*-----添加UI-----房间预订信息-----*/
    @RequestMapping("/customer/addUI")
    public String customerAddUI(Model model){
        System.out.println("===添加UI===addUI()===");
        Subject currentUser = SecurityUtils.getSubject();
        //获取当前用户username
        String username = (String) currentUser.getSession().getAttribute("username");
        User user = iUserService.queryByUsername(username);
        model.addAttribute("user", user);
        return "system/customer/roomOrder/addRoomOrder1";
    }

    @RequestMapping("/customer/add")
    @ResponseBody
    public Map<String,Object> customerAdd(Model model, RoomOrder roomOrder){
        Map<String, Object> map = new HashMap<>();
        boolean b = iRoomOrderService.save(roomOrder);
        if(b) {
            map.put("code",200);
        } else{
            map.put("code",500);
        }
        return map;
    }

    //删除 ->客户取消预订
    @RequestMapping("/customer/delete")
    @ResponseBody
    public Map<String,Object> customerDelete(int roomOrderId){
        Map<String,Object> map=new HashMap<>();
        System.out.println(roomOrderId);
        boolean b=iRoomOrderService.removeById(roomOrderId);
        if(b){
            map.put("code",200);
        }else {
            map.put("code",500);
        }
        return map;
    }
    /**
     *添加一个订单
     * @author longyajr
     * @param roomOrder
     * @return
     */

    @RequestMapping("/admin/add")
    @ResponseBody
    public Map<String,Object> add(Model model, RoomOrder roomOrder){
        System.out.println("----------------add----------------");
        Map<String,Object> map=new HashMap<>();
        boolean b=iRoomOrderService.save(roomOrder);
        if(b){
            map.put("code",200);
        }else{
            map.put("code",500);
        }
        return map;
    }

    /*-----删除-----通过roomOrderId删除房间具体信息-----*/
//    @RequestMapping("/admin/delete")
//    public String delete(Model model,int roomOrderId){
//        //获取数据
//        System.out.println(roomOrderId);
//        //调用业务
//        boolean b=iRoomOrderService.removeById(roomOrderId);
//        //返回roomOrderList页面
//        List<RoomOrder> roomOrderList_db=iRoomOrderService.list();
//        model.addAttribute("roomOrderList_db",roomOrderList_db);
//        System.out.println("===删除房间预订信息===delete()===");
//        return "system/admin/roomOrder/roomOrderList";
//    }

    /**
     * 删除一个订单
     * @author longyajr
     * @param roomOrderId
     * @return
     */
    @RequestMapping("/admin/delete")
    @ResponseBody
    public Map<String,Object> delete(int roomOrderId){
        Map<String,Object> map=new HashMap<>();
        boolean b=iRoomOrderService.removeById(roomOrderId);
        if(b){
            map.put("code",200);
        }else {
            map.put("code",500);
        }
        return map;
    }


    /*-----修改UI-----房间预订信息-----*/
    @RequestMapping("/admin/updateUI")
    public String updateUI(Model model,int roomOrderId){
        //接收数据
//        System.out.println(roomOrderId);
        //调用业务
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        //通过 roomOrderId房间预订信息ID查询对应的房间预订信息
        RoomOrder roomOrder_db=iRoomOrderService.getById(roomOrderId);
        model.addAttribute("roomOrder_db",roomOrder_db);
        model.addAttribute("checkInTime",simpleDateFormat.format(roomOrder_db.getCheckInTime()));
        model.addAttribute("checkOutTime",simpleDateFormat.format(roomOrder_db.getCheckOutTime()));
//        System.out.println("===修改UI===updateUI()===");
        //返回updateRoomOrder页面
        return "system/admin/roomOrder/updateRoomOrder1";
    }

    /**
     * 修改一个订单
     * @author longyajr
     * @param roomOrder
     * @return
     */
    @RequestMapping("/admin/update")
    @ResponseBody
    public Map<String,Object> update(RoomOrder roomOrder){
        Map<String,Object> map=new HashMap<>();
        boolean b=iRoomOrderService.saveOrUpdate(roomOrder);
        if(b){
            map.put("code",200);
            if(roomOrder.getOrderState() == 1){
                checkOut(roomOrder.getRoomOrderId());
            }
        }else {
            map.put("code",500);
        }
        return map;
    }
    /*-----修改-----房间预订信息-----*/
//    @RequestMapping("/update")
//    public String update(Model model,RoomOrder roomOrder, String inTime, String outTime){
//        //获取数据
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime dateTime = LocalDateTime.parse(inTime, dateTimeFormatter);
//        roomOrder.setCheckInTime(dateTime);
//
//        dateTime = LocalDateTime.parse(outTime, dateTimeFormatter);
//        roomOrder.setCheckOutTime(dateTime);
//        System.out.println(roomOrder);
//
//        //调用业务进行更新
//        boolean b=iRoomOrderService.saveOrUpdate(roomOrder);
//        //返回roomOrderList页面
//        List<RoomOrder> roomOrderList_db=iRoomOrderService.list();
//        model.addAttribute("roomOrderList_db",roomOrderList_db);
//        System.out.println("===添加房间预订信息===update()===");
//        return "system/admin/roomOrder/roomOrderList";
//    }
    @RequestMapping("/update")
    public String update(Model model,RoomOrder roomOrder, String inTime, String outTime) throws ParseException {
        //获取数据
        //获取开始打扫时间，打扫结束时间
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date localDateTime = simpleDateFormat.parse(inTime);
        roomOrder.setCheckInTime(localDateTime);

        localDateTime=simpleDateFormat.parse(outTime);
        roomOrder.setCheckOutTime(localDateTime);
        System.out.println(roomOrder);

        //调用业务进行更新
        boolean b=iRoomOrderService.saveOrUpdate(roomOrder);
        //返回roomOrderList页面
        List<RoomOrder> roomOrderList_db=iRoomOrderService.list();
        model.addAttribute("roomOrderList_db",roomOrderList_db);
        System.out.println("===添加房间预订信息===update()===");
        return "system/admin/roomOrder/roomOrderList";
    }

    /**
     *
     * @author longyajr
     */

//    @RequestMapping("/admin/list")
//    public String adminList(Model model) {
//        List<RoomOrder> roomOrderList_db=iRoomOrderService.list();
//        model.addAttribute("roomOrderList_db",roomOrderList_db);
//        return "system/admin/roomOrder/roomOrderList";
//    }
        /*//调用业务进行更新
        boolean b=iRoomOrderService.saveOrUpdate(roomOrder);
        //返回roomOrderList页面
        List<RoomOrder> roomOrderList_db=iRoomOrderService.list();
        model.addAttribute("roomOrderList_db",roomOrderList_db);
        System.out.println("===添加房间预订信息===update()===");
        return "system/admin/roomOrder/roomOrderList";
    }*/

    /**
     * @author treasy
     * @description 退房操作并且生成清洁订单
     */
    @RequestMapping("/admin/check-out")
    @ResponseBody
    public Map<String, Object> checkOut(int roomOrderId){
        Map<String, Object> map=new HashMap<>();
        //更新房间订单信息
        int flag = iRoomOrderService.updateStateById(roomOrderId);
        //根据订单号获取房间id
        RoomOrder roomOrder = iRoomOrderService.getById(roomOrderId);
        Integer roomId = roomOrder.getRoomId();
        //更新房间状态
        int flag1 = iRoomService.updateStateById(roomId,-1);
        //获取记录时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String recordTime=simpleDateFormat.format(date);
        //生成清洁订单
        iCleaningRecordService.insertOne(roomId,recordTime);
        return null;
    }
}
